/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Design/ITOPVARRepository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace TOPVAR
{
    struct IDENTITY
    {
        SG::DataStructure::Common::Id   TOPID      = 0;      ///<
        char                            LABEL[8]   = { 0 };  ///<
        char                            PTYPE[8]   = { 0 };  ///<
        SG::DataStructure::Common::Real XINIT      = 0.0;    ///<
        SG::DataStructure::Common::Real XLB        = 0.0;    ///<
        SG::DataStructure::Common::Real DELXV      = 0.0;    ///<
        SG::DataStructure::Common::Real POWER      = 0.0;    ///<
        SG::DataStructure::Common::Id   PID        = 0;      ///<
        SG::DataStructure::Common::I64  SYM_LEN    = 0;      ///<
        SG::DataStructure::Common::I64  CAST_LEN   = 0;      ///<
        SG::DataStructure::Common::I64  EXT_LEN    = 0;      ///<
        SG::DataStructure::Common::I64  MIN_LEN    = 0;      ///<
        SG::DataStructure::Common::I64  STRESS_LEN = 0;      ///<
        SG::DataStructure::Common::I64  PRINT_LEN  = 0;      ///<
        SG::DataStructure::Common::I64  SYM_POS    = 0;      ///<
        SG::DataStructure::Common::I64  CAST_POS   = 0;      ///<
        SG::DataStructure::Common::I64  EXT_POS    = 0;      ///<
        SG::DataStructure::Common::I64  MIN_POS    = 0;      ///<
        SG::DataStructure::Common::I64  STRESS_POS = 0;      ///<
        SG::DataStructure::Common::I64  PRINT_POS  = 0;      ///<
        SG::DataStructure::Common::Id   DOMAIN_ID  = 0;      ///<
    };
};  // namespace TOPVAR
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::TOPVAR::IDENTITY> ()
{
    HighFive::CompoundType compoundType = { { "TOPID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "LABEL", HighFive::create_datatype<char[8]> (), 1, true },
                                            { "PTYPE", HighFive::create_datatype<char[8]> (), 1, true },
                                            { "XINIT", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "XLB", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "DELXV", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "POWER", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                            { "PID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                            { "SYM_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "CAST_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "EXT_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "MIN_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "STRESS_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "PRINT_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "SYM_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "CAST_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "EXT_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "MIN_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "STRESS_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "PRINT_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                            { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::Id> () } };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::TOPVARData::CASTData> ()
{
    HighFive::CompoundType ct = { { "CID2", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "DDI", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "DIE", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "ALIGN", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::TOPVARData::EXTRUSIONData> ()
{
    HighFive::CompoundType ct = { { "CID3", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "EDI", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "ALIGN2", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::TOPVARData::MINData> ()
{
    HighFive::CompoundType ct = { { "TDMIN", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                  { "TDMAX", HighFive::create_datatype<SG::DataStructure::Common::Real> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::TOPVARData::PRINTData> ()
{
    HighFive::CompoundType ct = { { "CID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "PRINTD", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "ALIGN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "OHANGLE", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                  { "OHPMETHOD", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "DENCUT", HighFive::create_datatype<SG::DataStructure::Common::Real> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::TOPVARData::STRESSData> ()
{
    HighFive::CompoundType ct = { { "STLIM", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                  { "PNORM", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                  { "PSFT", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
                                  { "PMETHOD", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "PMATCH", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::TOPVARData::SYMPLANEData> ()
{
    HighFive::CompoundType ct = { { "CID", HighFive::create_datatype<SG::DataStructure::Common::Id> () },
                                  { "MS1", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "MS2", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "MS3", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "CSM", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
                                  { "NCSM", HighFive::create_datatype<SG::DataStructure::Common::I64> () } };
    return ct;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class TOPVARRepository : public ITOPVARRepository, public SG::DBManager::HDF5::MainlyRepositoryBase<SG::DataStructure::FEM::TOPVARData>
{
    using MainStruct      = SG::DataStructure::FEM::TOPVARData;
    using SubStructCAST   = SG::DataStructure::FEM::TOPVARData::CASTData;
    using SubStructEXT    = SG::DataStructure::FEM::TOPVARData::EXTRUSIONData;
    using SubStructMIN    = SG::DataStructure::FEM::TOPVARData::MINData;
    using SubStructPRINT  = SG::DataStructure::FEM::TOPVARData::PRINTData;
    using SubStructSTRESS = SG::DataStructure::FEM::TOPVARData::STRESSData;
    using SubStructSYM    = SG::DataStructure::FEM::TOPVARData::SYMPLANEData;

    using NasMainStruct = SG::DBManager::HDF5::TOPVAR::IDENTITY;

  public:
    explicit TOPVARRepository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset_CAST,
        SubDataset_EXT,
        SubDataset_MIN,
        SubDataset_PRINT,
        SubDataset_STRESS,
        SubDataset_SYM
    };

    /**
     * @brief   : 初始化表数据
     * @author  : wuxingchuang
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief   : 获取所有数据
     */
    std::vector<MainStruct> GetAllData () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<MainStruct>&           own,
                       std::vector<NasMainStruct>& _OUT   IDENTITYData,
                       std::vector<SubStructCAST>& _OUT   subDataCAST,
                       std::vector<SubStructEXT>& _OUT    subDataEXT,
                       std::vector<SubStructMIN>& _OUT    subDataMIN,
                       std::vector<SubStructPRINT>& _OUT  subDataPRINT,
                       std::vector<SubStructSTRESS>& _OUT subDataSTRESS,
                       std::vector<SubStructSYM>& _OUT    subDataSYM,

                       bool isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITYData
     * @param   : subData
     * @param   : own
     */
    void NastranToOwn (std::vector<NasMainStruct>&   IDENTITYData,
                       std::vector<SubStructCAST>&   subDataCAST,
                       std::vector<SubStructEXT>&    subDataEXT,
                       std::vector<SubStructMIN>&    subDataMIN,
                       std::vector<SubStructPRINT>&  subDataPRINT,
                       std::vector<SubStructSTRESS>& subDataSTRESS,
                       std::vector<SubStructSYM>&    subDataSYM,
                       std::vector<MainStruct>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
